﻿using Curry.Forum.Infrastructure;
using Curry.Forum.Query.Service.DTOS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Curry.Forum.Query.Service.Dapper
{
    public class ArticleQueryService :  IArticleQueryService
    {
        private DBHelper _sqlClient = new DBHelper();

        public ArticleInfo GetArticleById(string articleId)
        {
            var model = _sqlClient.Query<ArticleInfo>(ArticleInfo.GetSql+ " AND Id=@Id", new { Id = articleId }).FirstOrDefault();
            if (model == null)
                return null;

            return model;
        }

        public ArticleDate GetArticleList(int pageIndex, int pageSize)
        {
            ArticleDate date = new ArticleDate();
            var start = (pageIndex - 1) * pageSize + 1;
            var end = pageIndex * pageSize;

            var model = _sqlClient.Query<ArticleInfo>(string.Format(ArticleInfo.GetPagingSql, start, end)).ToList();
            if (model == null)
                return null;

            var TotalNum = _sqlClient.Query<string>(string.Format(ArticleInfo.GetCountSql)).FirstOrDefault();

            date.TotalCount = Convert.ToInt32(TotalNum);
            date.ArticleList = model;

            return date;
        }
    }
}
